﻿using HidSharp.Experimental;
using HidSharp.Reports;
using HidSharp.Reports.Encodings;
using HidSharp.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace customized_keyboard
{
    class hiddef
    {
        public HidSharp.DeviceList GetHidDevice()
        {
            HidSharpDiagnostics.EnableTracing = true;
            HidSharpDiagnostics.PerformStrictChecks = true;
            var list = HidSharp.DeviceList.Local;
            list.Changed += (sender, e) => Console.WriteLine("Device list changed.");
            return list;
        }

        /// <summary>
        /// 截取字符串中开始和结束字符串中间的字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>中间字符串</returns>
        public string SubstringSingle(string source, string startStr, string endStr)
        {
            Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
            return rg.Match(source).Value;
        }

        /// <summary>
        /// （批量）截取字符串中开始和结束字符串中间的字符串
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="startStr">开始字符串</param>
        /// <param name="endStr">结束字符串</param>
        /// <returns>中间字符串</returns>
        public List<string> SubstringMultiple(string source, string startStr, string endStr)
        {
            Regex rg = new Regex("(?<=(" + startStr + "))[.\\s\\S]*?(?=(" + endStr + "))", RegexOptions.Multiline | RegexOptions.Singleline);
            MatchCollection matches = rg.Matches(source);
            List<string> resList = new List<string>();
            foreach (Match item in matches)
                resList.Add(item.Value);
            return resList;
        }



        public void WriteXml(string str)
        {
            //创建一个数据集，将其写入xml文件
            string optime = "config" + ".xml";
            System.Data.DataSet ds = new System.Data.DataSet("MESSAGE");
            System.Data.DataTable table = new System.Data.DataTable("TABLE");
            ds.Tables.Add(table);
            table.Columns.Add("ID", typeof(string));

            System.Data.DataRow row = table.NewRow();
            row[0] = str;
            ds.Tables["TABLE"].Rows.Add(row);

            string path = optime;
            ds.WriteXml(path);
        }
    }
}
